package com.unhook.client;

import com.google.gwt.core.client.EntryPoint;
import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.data.fields.DataSourceFloatField;
import com.smartgwt.client.data.fields.DataSourceIntegerField;
import com.smartgwt.client.data.fields.DataSourceTextField;
import com.smartgwt.client.types.DSDataFormat;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.HTMLFlow;
import com.smartgwt.client.widgets.IButton;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.VLayout;
import com.smartgwt.client.widgets.tree.TreeGrid;
import com.smartgwt.client.widgets.tree.TreeGridField;
import com.smartgwt.client.widgets.tree.events.DataArrivedEvent;
import com.smartgwt.client.widgets.tree.events.DataArrivedHandler;
 
 public class myWidget implements EntryPoint {
    /**
     * This is the entry point method.
     */
    public void onModuleLoad() {
    	Canvas layout = new Canvas();
    	final TreeGrid tree = new TreeGrid();
        tree.setLoadDataOnDemand(true);        
        tree.setWidth(500);
        tree.setHeight(400);
        tree.setDataSource(new Filer());
        tree.setAutoFetchData(true);
        tree.setShowConnectors(true);       
        tree.setShowOpenIcons(false);
        tree.setShowDropIcons(false);
        tree.setClosedIconSuffix("");     
        tree.setBaseStyle("noBorderCell");
        tree.setFields(new TreeGridField("file"));
/*
        tree.addDataArrivedHandler(new DataArrivedHandler() {
            public void onDataArrived(DataArrivedEvent event) {
                tree.getData().openAll();
            }
        });
        */
        layout.addChild(tree);

        layout.draw();
        
        SC.showConsole();
    }
    
    /*
     * Provide Files/Directory view
     */
    static class Filer extends DataSource{
    	private static DataSource instance;
    	
		public static DataSource build(String nodePath) {
			
			if(instance == null){
				instance = new Filer();
			}
			return instance;
		}
		
		public Filer() {
				setID("hello");
				setTitleField("name");
		        setRecordXPath("/system/file");
		        DataSourceTextField nameField = new DataSourceTextField("name", "name", 128);

		        DataSourceIntegerField employeeIdField = new DataSourceIntegerField("node", "node");
		        employeeIdField.setPrimaryKey(true);
		        employeeIdField.setRequired(true);

		        DataSourceIntegerField reportsToField = new DataSourceIntegerField("root", "root");
		        reportsToField.setRequired(true);
		        reportsToField.setForeignKey(id+".node");
		        reportsToField.setRootValue("1");

		        DataSourceTextField jobField = new DataSourceTextField("path", "Path", 128);

		        setFields(nameField, employeeIdField, reportsToField, jobField);

		        setDataURL("filer/show");
		        setDataFormat(DSDataFormat.XML);
		        setClientOnly(false);
		}
    	
    }
}
